package org.mq.mymq.broker.config;

import io.netty.util.internal.StringUtil;
import org.mq.mymq.broker.cache.CommonCache;
import org.mq.mymq.common.constants.BrokerConstants;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @ClassName GlobalPropertiesLoader
 * @Description 全局配置属性加载器
 * @Author jiarong_ye
 * @Date 2025/6/15 16:03
 * @Version 1.0
 */
public class GlobalPropertiesLoader {
    private GlobalProperties globalProperties;
    public void loadProperties() {
        globalProperties = new GlobalProperties();
        String myMqHome = System.getenv(BrokerConstants.MY_MQ_HOME);
        if (StringUtil.isNullOrEmpty(myMqHome)) {
            throw new IllegalArgumentException("MY_MQ_HOME is not set");
        }
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(new File(myMqHome + BrokerConstants.BASE_PROPERTIES_PATH)));
            globalProperties.setNameserverIp(properties.getProperty("nameserver.ip"));
            globalProperties.setNameserverPort(Integer.valueOf(properties.getProperty("nameserver.port")));
            globalProperties.setNameserverUser(properties.getProperty("nameserver.user"));
            globalProperties.setNameserverPassword(properties.getProperty("nameserver.password"));
            globalProperties.setBrokerPort(Integer.valueOf(properties.getProperty("broker.port")));
            globalProperties.setReBalanceStrategy(properties.getProperty("rebalance.strategy"));
            // 可以为null TODO
            globalProperties.setBrokerClusterGroup(properties.getProperty("broker.cluster.group"));
            globalProperties.setBrokerClusterMode(properties.getProperty("broker.cluster.mode"));
            globalProperties.setBrokerClusterRole(properties.getProperty("broker.cluster.role"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        globalProperties.setMyMqHome(myMqHome);
        CommonCache.setGlobalProperties(globalProperties);
    }
}
